package com.zy.algorithm.sort.shellsort;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.graphics.Color;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;

import com.zy.algorithm.R;
import com.zy.algorithm.controller.SubSetController;
import com.zy.algorithm.controller.screen.IScreenController;
import com.zy.algorithm.controller.screen.ShellInsertionChildScreenController;
import com.zy.algorithm.curtain.IScreenBean;
import com.zy.algorithm.curtain.ShellInsertionScreenBean;
import com.zy.algorithm.sort.insertion.InsertionSortBaseFragment;
import com.zy.view.SubSetTipsView;

import java.util.ArrayList;
import java.util.List;

public abstract class ShellInsertionSortBaseFragment extends InsertionSortBaseFragment {

    private static final String TAG = "ShellInsertionSortBase";
    private SubSetController subSetController;

    FrameLayout child_screen_ui_container;

    @Override
    protected void initView(View view) {
        super.initView(view);

        child_screen_ui_container = view.findViewById(R.id.child_screen_ui_container);

        indexController.hideAllIndex();
        sort_index_tips_container.setVisibility(View.GONE);

        subSetController = new SubSetController(view);
        subSetController.show();
    }

    //ShellInsertionScreenBean{needScreenAnimation=true, gap=5, subSetIndexList=[0, 5], subSetStart=[49, 38], subSetEnd=[38, 49]}
    //ShellInsertionScreenBean{needScreenAnimation=true, gap=5, subSetIndexList=[1, 6], subSetStart=[77, 83], subSetEnd=[77, 83]}
    //ShellInsertionScreenBean{needScreenAnimation=true, gap=5, subSetIndexList=[2, 7], subSetStart=[61, 43], subSetEnd=[43, 61]}
    //ShellInsertionScreenBean{needScreenAnimation=true, gap=5, subSetIndexList=[3, 8], subSetStart=[97, 43], subSetEnd=[43, 97]}
    //ShellInsertionScreenBean{needScreenAnimation=true, gap=5, subSetIndexList=[4, 9], subSetStart=[15, 71], subSetEnd=[15, 71]}
    //ShellInsertionScreenBean{needScreenAnimation=true, gap=2, subSetIndexList=[0, 2, 4, 6, 8], subSetStart=[38, 43, 15, 83, 97], subSetEnd=[15, 38, 43, 83, 97]}
    //ShellInsertionScreenBean{needScreenAnimation=true, gap=2, subSetIndexList=[1, 3, 5, 7, 9], subSetStart=[77, 43, 49, 61, 71], subSetEnd=[43, 49, 61, 71, 77]}
    //ShellInsertionScreenBean{needScreenAnimation=false, gap=1, subSetIndexList=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], subSetStart=[15, 43, 38, 49, 43, 61, 83, 71, 97, 77], subSetEnd=[15, 38, 43, 43, 49, 61, 71, 77, 83, 97]}

    //幕 -> 切换动画
    //1. 分组动画
    //2. 分组动画结束 - 开始screen动画
    //2. 幕-切换动画
    @Override
    protected void sortAnimation(int index, IScreenBean screenBean, StepListener listener) {
        //super.sortAnimation(index, screenBean, listener);

        if (!(screenBean instanceof ShellInsertionScreenBean)) {
            return;
        }


        ShellInsertionScreenBean bean = (ShellInsertionScreenBean) screenBean;

        setScreenTxtDes("" + bean.getDescribe());

        Log.d(TAG, "" + bean);

        List<Integer> subSetIndexList = bean.getSubSetIndexList();

        ShellInsertionChildScreenController shellInsertionChildScreenController = null;
        if (screenBean.isNeedScreenAnimation()) {
            shellInsertionChildScreenController = new ShellInsertionChildScreenController(child_screen_ui_container);
            shellInsertionChildScreenController.setScreenBean(bean);
        } else {
            indexController.hideAllIndex(false, true);
        }

        ShellInsertionChildScreenController finalShellInsertionChildScreenController = shellInsertionChildScreenController;
        //分组动画
        subSetController.showSubSet(toSubSetNode(subSetIndexList), new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);

                if (finalShellInsertionChildScreenController != null) {
                    finalShellInsertionChildScreenController.startPageEnterAnimator(firstScreenController,
                            bean,
                            firstScreenController.getSubSetIndexListView(subSetIndexList),
                            listener
                    );
                } else {
                    //
                    firstScreenController.startSubCartoon(bean.getCartoonStepBeanList(), listener);
                }
            }
        });
    }

    private List<SubSetTipsView.SubSetNode> toSubSetNode(List<Integer> subSetIndexList) {
        List<SubSetTipsView.SubSetNode> nodes = new ArrayList<>();


        int heightBase = 100;
        int[] BASE_COLORS = new int[]{Color.RED, Color.BLUE, Color.GREEN, Color.MAGENTA, Color.CYAN, Color.RED, Color.RED};

        List<Integer> xPointList = new ArrayList<>();


        //下标转换成-px
        for (Integer index : subSetIndexList) {
            xPointList.add(getCenterXByIndex(index) - 30);
        }

        SubSetTipsView.SubSetNode node = new SubSetTipsView.SubSetNode(xPointList, heightBase, BASE_COLORS[0]);
        nodes.add(node);

        return nodes;
    }
}
